package com.example.teststarter.controller;

import com.example.teststarter.designpattern.Book;
import com.example.teststarter.designpattern.LibraryFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/studentController")
public class StudentController {

    /**
     * 图书馆书架上的书
     */
    private static List<Book> books = new ArrayList<>();
    private static LibraryFactory libraryFactory;

    /**
     * 查询
     */
    @PostMapping("/borrow")
    @ResponseBody
    public void borrow() {
        libraryFactory = LibraryFactory.getInstance();
        studentBorrow("葵花宝典");
        studentBorrow("僻邪剑谱");
        studentBorrow("java从入门到放弃");
        //把每一本书借出去
        for (Book book : books) {
            book.borrow();
        }
        System.out.println("===========================后两本秘籍没学会，又借了一次 ");
        studentBorrow("僻邪剑谱");
        studentBorrow("java从入门到放弃");

        Book piXieJianPu = libraryFactory.libToBorrow("僻邪剑谱");
        piXieJianPu.borrow();
        Book ruMenFangQi = libraryFactory.libToBorrow("java从入门到放弃");
        ruMenFangQi.borrow();

        //输出一些学生一共借多少本书
        System.out.println("学生一共借了 " + books.size() + " 本书! ");
        //输出一下图书馆一共借出多少本书
        System.out.println("图书馆实际借出" + libraryFactory.getAllBookSize() + " 本书");
    }

    /**
     * 学生借书，将借的书，记录到图书馆书架列表
     */
    private static void studentBorrow(String bookName) {
        books.add(libraryFactory.libToBorrow(bookName));
    }
}
